package net.sk.china.platform.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sk.china.platform.request.AdminLoginRequest;
import net.sk.china.platform.request.ValidateCodeRequest;
import net.sk.china.platform.request.system.AdminResponse;
import net.sk.china.platform.response.system.LoginResponse;
import net.sk.china.platform.service.AdminService;
import net.sk.china.platform.utils.MenusResponse;
import net.sk.china.common.response.CommonResult;
import net.sk.china.common.utils.TalkUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2018-2024 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2024/6/2 10:03
 * // +----------------------------------------------------------------------
 */

@RestController
@RequestMapping("/")
@Api(tags = "登录相关服务")
public class AdminController {

    private final AdminService adminService;

    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }

    @ApiOperation(value = "登录")
    @RequestMapping(value = "login", method = RequestMethod.POST, produces = "application/json")
    public CommonResult<LoginResponse> adminLogin(@RequestBody @Validated AdminLoginRequest adminLoginRequest, HttpServletRequest request)
    {
        String ip = TalkUtil.getClientIp(request);
        LoginResponse response = adminService.login(adminLoginRequest, ip);
        return CommonResult.success(response, "登录成功");
    }

    @ApiOperation(value = "获取登录相关图片")
    @RequestMapping(value = "getLoginPic", method = RequestMethod.GET)
    public CommonResult<Map<String, Object>> getLoginPic()
    {
        return CommonResult.success(adminService.getLoginPic());
    }


    @ApiOperation(value = "获取验证码")
    @RequestMapping(value = "validate/code", method = RequestMethod.GET)
    public CommonResult<ValidateCodeRequest> code()
    {
        return  CommonResult.success(adminService.getValidateCode());
    }

    @ApiOperation(value = "退出登录")
    @RequestMapping(value = "admin/logout", method = RequestMethod.GET)
    public CommonResult<String> logout()
    {
        adminService.logout();
        return CommonResult.success("退出成功");
    }

    @PreAuthorize("hasAuthority('admin:info')")
    @ApiOperation(value="获取用户详情")
    @GetMapping(value = "getAdminInfoByToken")
    public CommonResult<AdminResponse> getAdminInfo() {
        return CommonResult.success(adminService.getInfoByToken());
    }

    /**
     * 获取管理员可访问目录
     */
    @PreAuthorize("hasAuthority('admin:login:menus')")
    @ApiOperation(value = "获取管理员可访问目录")
    @RequestMapping(value = "getMenus", method = RequestMethod.GET)
    public CommonResult<List<MenusResponse>> getMenus() {
        return CommonResult.success(adminService.getMenus());
    }
}
